﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace zmMoniter
{
    public partial class frmWarning : DevExpress.XtraEditors.XtraForm
    {
        public mainForm main = null;
        public frmWarning(mainForm frm)
        {
            InitializeComponent();
            this.main = frm;
        }
        private void smpBtn_OK_Click(object sender, EventArgs e)
        {
            reSetData();
            this.Hide();
        }
        private void reSetData()
        {
            chkEdt_IgnoreAll.Checked = false;
            smpBtn_OK.Visible = false;

            labelCtl_Rectification_Value.Text = "";
            labelCtl_Rectification_Value.Enabled = true;
            chkEdt_Ignore.Checked = false;

            labelCtl_ID.Text = "";
            labelCtl_DamBlock_Value.Text = "";

            labelCtl_Deck_Value.Text = "";

            labelCtl_Device_Value.Text = "";
            labelCtl_DateTime_Value.Text = "";
            textEdt_WarnText_Value.Text = "";

        }
        public bool reRefush(string sSQL)
        {
            bool bRst = false;
            if (sSQL == "")
                return bRst;
            PublicDataCls db = new PublicDataCls();
            string sSQLState = db.OpenConn();
            if (sSQLState != "")
            {
                this.Hide();
                return bRst;
            }
            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();

            this.gridCtl_Warning.DataSource = dt;
          
            if (dt == null || dt.Rows.Count == 0)
            {
                this.Hide();
                this.main.upDateWarn(false);
                return bRst;
            }
            else
            {
                bRst = true;
                gridView1.FocusedRowHandle = -1;
                this.gridView1.MoveFirst();
                label_msg.Text = "你共有" + dt.Rows.Count + "条预警信息等待处理!";
                updateWarnDeck(ref dt);
            }

            bRst = true;
            return bRst;

        }
        private void updateWarnDeck(ref DataTable dt)
        {

            this.main.htWarnDeck.Clear();
            bool bResult = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                DataRow dataRow = dt.Rows[i];
                string sState = dataRow["处理状态"].ToString().Trim();
                if (sState != "0")
                    continue;
                bResult = true;
                string sID = dataRow["编号"].ToString().Trim();
                string sDeckID = dataRow["仓面号"].ToString().Trim();
                string sDeckName = dataRow["仓面名称"].ToString().Trim();
                string sDevice = dataRow["设备编号"].ToString().Trim();
                string sLayer = dataRow["铺层编号"].ToString().Trim();
                string sDateTime = dataRow["预警时间"].ToString().Trim();
                double dX = double.Parse(dataRow["X"].ToString().Trim());
                double dY = double.Parse(dataRow["Y"].ToString().Trim());
                string sDamBlock = sDeckName.Substring(0, sDeckName.LastIndexOf('-'));
                if (this.main.htWarnDeck.ContainsKey(sDamBlock))
                {
                    Hashtable htWarning = (Hashtable)this.main.htWarnDeck[sDamBlock];
                    Warning warn = new Warning(sID, sDeckName);
                    warn.setLayer(sLayer);
                    warn.setItemInfo(sDevice, sDateTime, dX, dY);
                    string sKey = sDeckName + "-" + sLayer;
                    if (htWarning.ContainsKey(sKey))
                    {
                        ArrayList alWarning = (ArrayList)htWarning[sKey];
                        alWarning.Add(warn);
                    }
                    else
                    {
                        ArrayList alWarning = new ArrayList();
                        alWarning.Add(warn);
                        htWarning.Add(sKey, alWarning);

                    }
                }
                else
                {
                    Hashtable htWarning = new Hashtable();
                    ArrayList alWarning = new ArrayList();
                    Warning warn = new Warning(sID, sDeckName);
                    warn.setLayer(sLayer);
                    warn.setItemInfo(sDevice, sDateTime, dX, dY);
                    alWarning.Add(warn);
                    htWarning.Add(sDeckName + "-" + sLayer, alWarning);
                    this.main.htWarnDeck.Add(sDamBlock, htWarning);
                }
            }
            this.main.upDateWarn(bResult);
        }
        public bool reLoadData(bool bFristLogin = false, string sID = "")
        {
            string sSQL = "";
            if (bFristLogin)
                sSQL = "select 预警表.GUID AS 编号," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间," +
                    "预警表.X,预警表.Y,仓面信息.仓面名称,预警表.仓面号,预警表.处理状态 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
                    "WHERE 预警表.处理状态=0 Order by 预警时间";
            else
            {
                string sItem = "AND GUID!='" + sID + "'";
                sSQL = "select 预警表.GUID AS 编号," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间," +
                    "预警表.X,预警表.Y,仓面信息.仓面名称,预警表.仓面号,预警表.处理状态 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
                    "WHERE 预警表.处理状态=0 Order by 预警时间";
            }

            return reRefush(sSQL);
        }
        public bool loadDate(string sDeckID, string sGUIDs)
        {
            string[] sGuid = sGUIDs.Split(',');
            string sSQLItem = "";

            for (int i = 0; i < sGuid.Length; i++)
            {
                if (i == 0)
                    sSQLItem += "预警表.GUID='" + sGuid[i] + "'";
                else
                    sSQLItem += "OR 预警表.GUID='" + sGuid[i] + "'";
            }
            string sSQL = "select 预警表.GUID AS 编号,预警表.仓面号," +
               "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
               "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间," +
               "预警表.X,预警表.Y,仓面信息.仓面名称,预警表.处理状态 " +
           "FROM 预警表 LEFT OUTER JOIN " +
           "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
           "WHERE 预警表.处理状态=0 AND (" + sSQLItem + ") Order by 预警时间";

            return reRefush(sSQL);
        }
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            int inRow = e.RowHandle;
            if (inRow < 0)
                return;
            //取得选定行信息  
            //string sID= gridView1.GetRowCellValue(inRow, "GUID").ToString();
            string sID = gridView1.GetRowCellValue(inRow, "编号").ToString();
            string sDeckID = gridView1.GetRowCellValue(inRow, "仓面号").ToString();
            string sDeckName = gridView1.GetRowCellValue(inRow, "仓面名称").ToString();
            string sLayer = gridView1.GetRowCellValue(inRow, "铺层编号").ToString();
            string sDevice = gridView1.GetRowCellValue(inRow, "设备编号").ToString();
            string sDate = gridView1.GetRowCellValue(inRow, "预警时间").ToString();
            string sWarnText = gridView1.GetRowCellValue(inRow, "备注").ToString();
            labelCtl_ID.Text = sID;
            string[] sDeck = sDeckName.Split('-');
            string sDamBlock = "";
            if (sDeck[1] == "S")
                sDamBlock = "上游坝块";
            else
                sDamBlock = "下游坝块";

            sDamBlock += "第" + sDeck[0] + "#坝段";
            labelCtl_DamBlock_Value.Text = sDamBlock;
            string sDeckCN = "";
            sDeckCN += sDeck[2] + "#仓面第 " + sLayer + " 铺层";
            labelCtl_Deck_Value.Text = sDeckCN;

            labelCtl_Device_Value.Text = sDevice;
            labelCtl_DateTime_Value.Text = sDate;
            textEdt_WarnText_Value.Text = sWarnText;

            //if (PrevFocusedRowHandle >= 0)
            //{
            this.main.setDateByLayer(sDeckName, sDeckID, sLayer);
            //}
        }
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int inRow = e.FocusedRowHandle;
            if (inRow < 0)
                return;

            //取得选定行信息  
            //string sID= gridView1.GetRowCellValue(inRow, "GUID").ToString();
            string sID = gridView1.GetRowCellValue(inRow, "编号").ToString();
            string sDeckID = gridView1.GetRowCellValue(inRow, "仓面号").ToString();
            string sDeckName = gridView1.GetRowCellValue(inRow, "仓面名称").ToString();
            string sLayer = gridView1.GetRowCellValue(inRow, "铺层编号").ToString();
            string sDevice = gridView1.GetRowCellValue(inRow, "设备编号").ToString();
            string sDate = gridView1.GetRowCellValue(inRow, "预警时间").ToString();
            string sWarnText = gridView1.GetRowCellValue(inRow, "备注").ToString();
            labelCtl_ID.Text = sID;
            string[] sDeck = sDeckName.Split('-');
            string sDamBlock = "";
            if (sDeck[1] == "S")
                sDamBlock = "上游坝块";
            else
                sDamBlock = "下游坝块";

            sDamBlock += "第" + sDeck[0] + "#坝段";
            labelCtl_DamBlock_Value.Text = sDamBlock;
            string sDeckCN = "";
            sDeckCN += sDeck[2] + "#仓面第 " + sLayer + " 铺层";
            labelCtl_Deck_Value.Text = sDeckCN;

            labelCtl_Device_Value.Text = sDevice;
            labelCtl_DateTime_Value.Text = sDate;
            textEdt_WarnText_Value.Text = sWarnText;

        }

        private void chkEdt_IgnoreAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEdt_IgnoreAll.Checked == false)
            {
                smpBtn_OK.Visible = false;
                return;
            }
            DialogResult dRst = MessageBox.Show("是否忽略所有警报?", "警报提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dRst == DialogResult.Yes)
            {
                if (chkEdt_IgnoreAll.Checked == true)
                {


                    bool bRst = handWarn(-9999, getAllIDs(), "");
                    if (bRst)
                    {
                        MessageBox.Show("警报处理成功！", "警报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Hide();
                        reSetData();
                    }
                    else
                        MessageBox.Show("警报处理失败！", "警报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    smpBtn_OK.Visible = false;
                }
            }
            else
            {
                if (chkEdt_IgnoreAll.Checked == true)
                    chkEdt_IgnoreAll.Checked = false;
                else
                    chkEdt_IgnoreAll.Checked = true;
            }
        }
        private string getAllIDs(string sExcloudID = "")
        {
            string sIDs = "";
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                string sID = gridView1.GetRowCellValue(i, "编号").ToString().Trim();
                if (sExcloudID == sID)
                    continue;
                if (sIDs != "" && sID != "")
                {
                    sIDs += "," + sID;
                }
                else if (sIDs == "" && sID != "")
                {
                    sIDs += sID;
                }
            }
            return sIDs;
        }
        private void chkEdt_Ignore_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEdt_Ignore.Checked == false)
                return;
            DialogResult dRst = MessageBox.Show("是否忽略此警报?", "警报提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dRst == DialogResult.Yes)
            {
                if (chkEdt_Ignore.Checked == true)
                {

                    string sID = labelCtl_ID.Text;
                    reSetData();
                    bool bRst = handWarn(-1, sID, "");
                    if (bRst)
                        MessageBox.Show("警报处理成功！", "警报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("警报处理失败！", "警报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                if (chkEdt_Ignore.Checked == true)
                    chkEdt_Ignore.Checked = false;
            }


        }
        private void sBtn_Hand_Click(object sender, EventArgs e)
        {
            DialogResult dRst = MessageBox.Show("是否已处理此警报?", "警报提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dRst == DialogResult.Yes)
            {
                string sID = labelCtl_ID.Text;
                string sContent = labelCtl_Rectification_Value.Text;
                reSetData();
                bool bRst = handWarn(1, sID, sContent);
                if (bRst)
                {
                    MessageBox.Show("警报处理成功！", "警报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                    MessageBox.Show("警报处理失败！", "警报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private bool handWarn(int flag, string sGUID, string sContent)
        {
            bool bResult = false;
            DateTime now = System.DateTime.Now;
            string sSQL = "";
            if (sGUID == "" && flag != -9999)
                return bResult;
            if (flag == -1)
            {
                sSQL = "Update 预警表 SET 处理状态=-1,处理时间=" +
                    "to_date('" + now.ToString() + "','YYYY/MM/DD HH24:MI:SS')" + ",处理人=" + main.sUserID + " WHERE GUID='" + sGUID + "'";

            }
            else if (flag == 1)
            {
                sSQL = "Update 预警表 SET 处理状态=1,处理时间=" +
                    "to_date('" + now.ToString() + "','YYYY/MM/DD HH24:MI:SS')" + ",处理人=" + main.sUserID + ",处理详情='" + sContent
                    + "'" + " WHERE GUID='" + sGUID + "'";
            }
            else if (flag == -9999)
            {
                string[] sID = sGUID.Split(',');
                string sSQLItem = "";
                for (int i = 0; i < sID.Length; i++)
                {
                    if (i == 0)
                        sSQLItem += "预警表.GUID='" + sID[i] + "'";
                    else
                        sSQLItem += "OR 预警表.GUID='" + sID[i] + "'";
                }
                sSQL = "Update 预警表 SET 处理状态=-1,处理时间=" +
                      "to_date('" + now.ToString() + "','YYYY/MM/DD HH24:MI:SS')" + ",处理人=" + main.sUserID + " WHERE (" + sSQLItem + ")";
                sGUID = "";
            }
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            int iResult = db.ExecuteSQL(sSQL);
            db.CloseConn();
            if (iResult > 0)
            {
                reLoadData(false, sGUID);
                bResult = true;
            }
            else
            {
                bResult = false;
            }
            return bResult;
        }
        private void warningTimer_Tick(object sender, EventArgs e)
        {
            if (reLoadData(true))
            {
                if (this.Visible == false)
                {
                    main.hideOrtherWind();
                    ShowDialog();
                }
                    
            }
            else
            {
                reSetData();
                Hide();
                main.clearWarning();
            }
        }
    }
}